package com.zhangchenhui.jmh;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.concurrent.TimeUnit;

/**
 * @author 张晨辉
 * @Classname JMHExample01
 * @Description TODO
 * @Date 2020/8/11 下午11:00
 */
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@BenchmarkMode(Mode.AverageTime)
@Threads(5)
@Fork(1)
@Measurement(iterations = 2)
@Warmup(iterations = 2)
@State(Scope.Benchmark)
public class JMHExample09 {
    // Level.Trial 执行一次
    // Level.Iteration 每次Warmup 和 每次Measurement 都会执行 setUp()和after()
    // Level.Invocation 每次基准测试 都会执行 setUp()和after()
    @Setup(Level.Invocation)
    public void setUp(){
        System.out.println("CREATE");
    }
    @Benchmark
    public void test() throws InterruptedException {
        TimeUnit.MILLISECONDS.sleep(1);
    }

    @TearDown(Level.Invocation)
    public void after(){
        System.out.println("DESTORY");
    }

    public static void main(String[] args) throws RunnerException {
        final Options opts = new OptionsBuilder()
                .include(JMHExample09.class.getSimpleName())
                .build();
        new Runner(opts).run();


    }
}
